package net.entelijan.cobean.examples.showcase.bind.text;

import static org.junit.Assert.assertEquals;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Test;

public class PathBindFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(PathBindFestTestCase.class);

	@Test
	public void testNextPreviousBook() {
		PathBindPanel panel = new PathBindPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			PathBindShowcase sc = new PathBindShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			final JTextComponentFixture bookTitleTextBox = frameFix
					.textBox("bookTitleTextField");
			final JButtonFixture nextBookButton = frameFix
					.button("nextBookButton");
			final JButtonFixture previousBookButton = frameFix
					.button("previousBookButton");

			final PathBindModel model = sc.getModel();

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Primo Levi", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Primo Levi");

			nextBookButton.click();
			pause(200);

			assertEquals("The Mystic Masseur", model.getBook().getTitle());
			assertEquals("V.S. Naipaul", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("The Mystic Masseur");
			authorNameTextBox.requireText("V.S. Naipaul");

			previousBookButton.click();
			pause(200);

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Primo Levi", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Primo Levi");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testNextPreviousDummyBook() {
		PathBindPanel panel = new PathBindPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			PathBindShowcase sc = new PathBindShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			final JTextComponentFixture bookTitleTextBox = frameFix
					.textBox("bookTitleTextField");
			final JButtonFixture nextBookButton = frameFix
					.button("nextBookButton");
			final JButtonFixture previousBookButton = frameFix
					.button("previousBookButton");
			final JButtonFixture setDummyAuthorButton = frameFix
					.button("setDummyAuthorButton");

			final PathBindModel model = sc.getModel();

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Primo Levi", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Primo Levi");

			setDummyAuthorButton.click();
			pause(200);

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Dummy Dummy", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Dummy Dummy");

			nextBookButton.click();
			pause(200);

			assertEquals("The Mystic Masseur", model.getBook().getTitle());
			assertEquals("V.S. Naipaul", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("The Mystic Masseur");
			authorNameTextBox.requireText("V.S. Naipaul");

			previousBookButton.click();
			pause(200);

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Dummy Dummy", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Dummy Dummy");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testNextPreviousDummyBookChangeFromGUI() {
		PathBindPanel panel = new PathBindPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			PathBindShowcase sc = new PathBindShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			final JTextComponentFixture bookTitleTextBox = frameFix
					.textBox("bookTitleTextField");
			final JButtonFixture nextBookButton = frameFix
					.button("nextBookButton");
			final JButtonFixture previousBookButton = frameFix
					.button("previousBookButton");
			final JButtonFixture setDummyAuthorButton = frameFix
					.button("setDummyAuthorButton");

			final PathBindModel model = sc.getModel();

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Primo Levi", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Primo Levi");

			setDummyAuthorButton.click();
			pause(200);

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Dummy Dummy", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Dummy Dummy");

			authorNameTextBox.deleteText();
			authorNameTextBox.setText("Dimitri Waluna");
			bookTitleTextBox.focus();
			pause(200);

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Dimitri Waluna", model.getBook().getAuthor()
					.getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Dimitri Waluna");

			nextBookButton.click();
			pause(200);

			assertEquals("The Mystic Masseur", model.getBook().getTitle());
			assertEquals("V.S. Naipaul", model.getBook().getAuthor().getName());
			bookTitleTextBox.requireText("The Mystic Masseur");
			authorNameTextBox.requireText("V.S. Naipaul");

			previousBookButton.click();
			pause(200);

			assertEquals("La chiave a stella", model.getBook().getTitle());
			assertEquals("Dimitri Waluna", model.getBook().getAuthor()
					.getName());
			bookTitleTextBox.requireText("La chiave a stella");
			authorNameTextBox.requireText("Dimitri Waluna");

		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
